Thread: [Help] Character pyramid with incrementing characters

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    10

    [Help] Character pyramid with incrementing characters

    Hi all,
    I am having some trouble with an assignment for one of my courses. We're supposed to print a character diamond, but not a diamond composed of just one character. It asks the user to input the character, and then that character is meant to be the border around the pyramid. In order to do this I get that for each new line, I need to be able to print the character and then increment it to the next character until it reaches halfway, then decrement the character until reaching the other side where we arrive at the original character.

    I know how to code a diamond of all the same character, but I'm just not getting how to get the input character to make a border around it.

    I have attached a picture of what the diamond should look like so if anyone has any suggestions pleaseeee chime in!

    [Help] Character pyramid with incrementing characters-capture-png

  2. #2
    Registered User
    Join Date
    Sep 2016
    Posts
    10
    So far this is my code for the diamond using all the same character. I think that the second nested for loop is gonna be where the action takes place to get the characters to change but I'm just not sure.
    Code:
    #include <stdio.h>
    
    int main(void){
        char usr_char;
        int usr_rows;
        int screen_width = 80;
        int i, j, k, space;
    
        printf("-----CHARACTER DIAMOND-----\n");
        printf("Enter a character: ");
        scanf("%c", &usr_char);
    
        printf("Enter an odd number between 1 and 40 (rows): ");
        scanf("%d", &usr_rows);
    
        //Print top half of diamond
        space = (screen_width / 2) - 1;
        for(i = 0; i < (usr_rows / 2) + 1; i++){
            for(j = 0; j < space; j++){
                printf(" ");
            }
            for(k = space + 1; k < (screen_width - space); k++){
                printf("%c", usr_char);
            }
            printf("\n");
            space -= 2;
        }
    
        //Print bottom half of diamond
        space = (screen_width / 2) - (usr_rows - 2);
        for(i = 0; i < (usr_rows / 2); i++){
            for(j = 0; j < space; j++){
                printf(" ");
            }
            for(k = space + 1; k < (screen_width - space); k++){
                printf("%c", usr_char);
            }
            printf("\n");
            space += 2;
        }
    
        return(0);
    }
    Last edited by agamemn0n; 09-28-2016 at 06:50 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Here's a hint
    Code:
            char c = usr_char;
            for(k = space + 1; k < (screen_width - space); k++){
                printf("%c", c++);
            }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2016
    Posts
    10
    Thanks so much Salem! Your tip really helped me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-18-2012, 02:26 PM
  2. incrementing character strings? pointers? HELP!
    By ominub in forum C Programming
    Replies: 10
    Last Post: 05-02-2009, 09:03 PM
  3. erase all characters in a character array?
    By diddy02 in forum C Programming
    Replies: 4
    Last Post: 11-20-2008, 05:30 PM
  4. Incrementing alpha characters. Base36
    By chops11 in forum C++ Programming
    Replies: 5
    Last Post: 07-18-2006, 02:56 PM

Tags for this Thread